﻿function getChoices(selectedControl, url, listName, fieldName) {
    $.ajax({
        url: url + "/_api/web/lists/getbytitle('" + listName + "')/fields/getbytitle('" + fieldName + "')",
        method: "GET",
        headers: { "Accept": "application/json; odata=verbose" },
        success: function (res) {
            var itemsBloc = $(selectedControl).children('.choice-bloc');
            for (var i in res.d.Choices.results) {
                $(itemsBloc).children('select').append('<option>' + res.d.Choices.results[i] + '</option>').on('change', function (e) {
                    var param = { $orderby: "Date" };
                    if ($(this).val() !== null && $(this).val() !== '') {
                        param = { $orderby: "Date", $top: 5, $filter: fieldName + " eq '" + $(this).val() + "'" };
                    }
                    getListItem($(selectedControl), url, listName, param);
                });
            }
        },
        error: function (data) {
        }
    });
}

function getListItem(selectedControl, url, listname, param) {
    $.ajax({
        url: url + "/_api/web/lists/getbytitle('" + listname + "')/items/",
        method: "GET",
        data: param,
        headers: { "Accept": "application/json; odata=verbose", "X-RequestDigest": $("#__REQUESTDIGEST").val() },
        success: function (res) {
            var itemsBloc = $(selectedControl).children('.items-bloc');
            $(itemsBloc).children('ul').empty();
            for (var item in res.d.results) {
                var lien = url + "/Pages/AnnonceDetails.aspx?ItemID=" + res.d.results[item].ID;
                if (res.d.results[item].Lien !== null && res.d.results[item].Lien !== undefined) {
                    lien = res.d.results[item].Lien.Url;
                }

                var bloc = createBloc(res.d.results[item].Title
                    , lien
                    , res.d.results[item].Photo.Url
                    , res.d.results[item].Chapeau);
                $(itemsBloc).children('ul').append('<li>' + bloc + '</li>');
            }
        },
        error: function (data) {
        }
    });
}


function createBloc(title, url, image, desc) {
    var content = "<div>";
    content += "		<div class='img-item'>";
    content += "		    <a href=" + url + ">";
    content += "		    	<img src='" + image + "' />";
    content += "		    </a>";
    content += "		</div>";
    content += "		<div class='info-item'>";
    content += "			<div class='title-item'>";
    content += "			    <a href=" + url + ">";
    content += "			    	<span>";
    content += "			        	<h3>";
    content += title;
    content += "			        	</h3>";
    content += "			    	</span>";
    content += "			    </a>";
    content += "			</div>";
    content += "			<div class='desc-item'>";
    content += "        		<p>";
    content += desc;
    content += "        		</p>";
    content += "			</div>";
    content += "		</div>";
    content += "</div>";
    return content;
}

$(function () {
    var url = "/relationpresse";
    var fieldName = "Pays";
    var param = { $orderby: "Date" };

    var actionRPListName = "Actions RP";
    getChoices($('#all-actus'), url, actionRPListName, fieldName);
    getListItem($('#all-actus'), url, actionRPListName, param);

    var presseEnParleList = "La presse en parle";
    //getChoices($('#all-actus'), url, presseEnParleList, fieldName);
    //getListItem($('#home-left'), url, presseEnParleList, param);
});